package com.example.demo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test03 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        Class aClass = Class.forName("com.ndky.User");
        user user = (com.example.demo.user) aClass.newInstance();
        Method setName = aClass.getDeclaredMethod("setName",String.class);
        setName.invoke(user,"zhang");
        System.out.println(user.getName());
        System.out.println("========================");

        Field age = aClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(user,18);
        System.out.println(user.getAge());
    }
}
